﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using Model;

namespace TaxiDispatchClient
{
    public partial class TaxiChargeConfigSetForm : DevExpress.XtraEditors.XtraForm
    {
        public TaxiChargeConfigSetForm()
        {
            InitializeComponent();
            BindConfig();
           
        }
         
         
        private Model.TaxiChargeInfo _model;
        

        private void TaxiParameterSetForm_Load(object sender, EventArgs e)
        { 

        }

       
        private void BindConfig()
        {
            _model = Common.OperateConfig.GetClientChargingModel();
            textEdit_DayEmptyBackDistance.Text =_model.DayEmptyBackDistance.ToString();
            textEdit_DayEmptyBackSubsidy.Text =_model.DayEmptyBackSubsidy.ToString();
            textEdit_DayOverPrice.Text =_model.DayOverPrice.ToString();
            textEdit_DayOverSubsidy.Text = _model.DayOverSubsidy.ToString();
            textEdit_DayWaiteTime.Text =_model.DayWaiteTime.ToString();
            textEdit_dayInitDistance.Text = _model.DayInitDistance.ToString();
            textEdit_dayInitPrice.Text = _model.DayInitPrice.ToString();
            textEdit_nightEmptyBackDistance.Text = _model.NightEmptyBackDistance.ToString();
            textEdit_nightEmptyBackSubsidy.Text =_model.NightEmptyBackSubsidy.ToString();
            textEdit_nightInitDistance.Text =_model.NightInitDistance.ToString();
            textEdit_nightInitPrice.Text = _model.NightInitPrice.ToString();
            textEdit_nightOverPrice.Text =_model.NightOverPrice.ToString();
            textEdit_nightOverSubsidy.Text =_model.NightOverSubsidy.ToString();
            textEdit_nightWaiteTime.Text =_model.NightWaiteTime.ToString();

        }

        private void btn_save_Click(object sender, EventArgs e)
        {
            if(btn_save.Tag.ToString()=="保存")
            {    
                if(!CheckPassed()) return;
                //// _taxiId 为-1时 设置出租车报警配置文件  不为 -1 时对单辆出租车设置
                //if (_taxiId == -1)
                //{
                //    string[] config = new string[6];
                //    config[0] = textEdit_speed.Text;
                //    config[1] = checkEdit_speed.Checked ? "1" : "0";
                //    config[2] = textEdit_stopTimeOut.Text;
                //    config[3] = checkEdit_stopTimeOut.Checked ? "1" : "0";
                //    config[4] = textEdit_power.Text;
                //    config[5] = checkEdit_power.Checked ? "1" : "0";
                //    Common.OperateConfig.SaveClientTaxiWarmingConfig(config);
                //    XtraMessageBox.Show("修改成功！");
                //}
                //else
                //{
                   //if(_model!=null)
                   //{
                TaxiChargeInfo model = new TaxiChargeInfo();
                model.DayEmptyBackDistance = float.Parse(textEdit_DayEmptyBackDistance.Text);
                model.DayEmptyBackSubsidy = float.Parse(textEdit_DayEmptyBackSubsidy.Text);
                model.DayInitDistance = float.Parse(textEdit_dayInitDistance.Text);
                model.DayInitPrice = float.Parse(textEdit_dayInitPrice.Text);
                model.DayOverPrice = float.Parse(textEdit_DayOverPrice.Text);

                model.DayOverSubsidy = float.Parse(textEdit_DayOverSubsidy.Text);
                model.DayWaiteTime = float.Parse(textEdit_DayWaiteTime.Text);
                model.NightEmptyBackDistance = float.Parse(textEdit_nightEmptyBackDistance.Text);
                model.NightEmptyBackSubsidy = float.Parse(textEdit_nightEmptyBackSubsidy.Text);
                model.NightInitDistance = float.Parse(textEdit_nightInitDistance.Text);
                model.NightInitPrice = float.Parse(textEdit_nightInitPrice.Text);
                model.NightOverPrice = float.Parse(textEdit_nightOverPrice.Text);
                model.NightWaiteTime = float.Parse(textEdit_nightWaiteTime.Text);
                model.NightOverSubsidy =float.Parse(textEdit_nightOverSubsidy.Text);


                Common.OperateConfig.SaveClientChargingConfig(model);                 
                XtraMessageBox.Show("修改成功！");
                                
                ResetControl(false);
                btn_save.Tag = "修改";
                btn_save.Image = Properties.Resources.UserForm_btnmodify001;
                btn_cancel.Visible = false;
            }
            else
            {
                ResetControl(true);
                btn_save.Tag = "保存";
                btn_save.Image = Properties.Resources.UserForm_btnsave001;
                btn_cancel.Visible = true;
            }
        }

        private void ResetControl(bool enable)
        {
            textEdit_DayEmptyBackDistance.Enabled = enable;
            textEdit_DayEmptyBackSubsidy.Enabled = enable;
            textEdit_DayOverPrice.Enabled = enable;
            textEdit_DayOverSubsidy.Enabled = enable;
            textEdit_DayWaiteTime.Enabled = enable;
            textEdit_dayInitDistance.Enabled = enable;
            textEdit_dayInitPrice.Enabled = enable;
            textEdit_nightEmptyBackDistance.Enabled = enable;
            textEdit_nightEmptyBackSubsidy.Enabled = enable;
            textEdit_nightInitDistance.Enabled = enable;
            textEdit_nightInitPrice.Enabled = enable;
            textEdit_nightOverPrice.Enabled = enable;
            textEdit_nightOverSubsidy.Enabled = enable;
            textEdit_nightWaiteTime.Enabled = enable;
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            btn_cancel.Visible = false;
            ResetControl(false);
            btn_save.Tag = "修改";
            btn_save.Image = Properties.Resources.UserForm_btnmodify001;
            BindConfig();
        }


        private bool CheckPassed()
        {
            if (textEdit_dayInitPrice.Text.Trim()==""||
                textEdit_dayInitPrice.Text=="0")
            {
                textEdit_dayInitPrice.Focus();
                XtraMessageBox.Show("请输入白班起步里程！");
                return false;
            }
            if (textEdit_dayInitDistance.Text.Trim()==""
                || textEdit_dayInitDistance.Text=="0")
            {
                textEdit_dayInitDistance.Focus();
                XtraMessageBox.Show("请输入白班起步价格！");
                return false;
            }
            if (textEdit_DayOverSubsidy.Text.Trim()==""||
                textEdit_DayOverSubsidy.Text=="0")
            {
                textEdit_DayOverSubsidy.Focus();
                XtraMessageBox.Show("请输入白班超出起步里程补贴！");
                return false;
            }
            if (textEdit_DayOverPrice.Text.Trim()==""||
                textEdit_DayOverPrice.Text=="0")
            {
                textEdit_DayOverPrice.Focus();
                XtraMessageBox.Show("请输入白班超出起步里程后每公里价格！");
                return false;
            }
            if (textEdit_DayEmptyBackDistance.Text.Trim()==""||
                textEdit_DayEmptyBackDistance.Text=="0")
            {
                textEdit_DayEmptyBackDistance.Focus();
                XtraMessageBox.Show("请输入空车返回公里数！");
                return false;
            }
            if (textEdit_DayEmptyBackSubsidy.Text.Trim()==""||
                textEdit_DayEmptyBackSubsidy.Text=="0")
            {
                textEdit_DayEmptyBackSubsidy.Focus();
                XtraMessageBox.Show("请输入空车返回补贴比例！");
                return false;
            }


            if (textEdit_nightInitPrice.Text.Trim() == "" ||
               textEdit_nightInitPrice.Text == "0")
            {
                textEdit_nightInitPrice.Focus();
                XtraMessageBox.Show("请输入晚班起步里程！");
                return false;
            }
            if (textEdit_nightInitDistance.Text.Trim() == ""
                || textEdit_nightInitDistance.Text == "0")
            {
                textEdit_nightInitDistance.Focus();
                XtraMessageBox.Show("请输入晚班起步价格！");
                return false;
            }
            if (textEdit_nightOverSubsidy.Text.Trim() == "" ||
                textEdit_nightOverSubsidy.Text == "0")
            {
                textEdit_nightOverSubsidy.Focus();
                XtraMessageBox.Show("请输入晚班超出起步里程补贴！");
                return false;
            }
            if (textEdit_nightOverPrice.Text.Trim() == "" ||
                textEdit_nightOverPrice.Text == "0")
            {
                textEdit_nightOverPrice.Focus();
                XtraMessageBox.Show("请输入晚班超出起步里程后每公里价格！");
                return false;
            }
            if (textEdit_nightEmptyBackDistance.Text.Trim() == "" ||
                textEdit_nightEmptyBackDistance.Text == "0")
            {
                textEdit_nightEmptyBackDistance.Focus();
                XtraMessageBox.Show("请输入晚班空车返回公里数！");
                return false;
            }
            if (textEdit_nightEmptyBackSubsidy.Text.Trim() == "" ||
                textEdit_nightEmptyBackSubsidy.Text == "0")
            {
                textEdit_nightEmptyBackSubsidy.Focus();
                XtraMessageBox.Show("请输入晚班空车返回补贴比例！");
                return false;
            }



           return true;
        }

        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PhoneUserForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
        #endregion



    }
}